﻿// Necroz project team © <2015> <ArchLord2EMU>
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Author:Sagara
// Date:2015-09-06-23:29
namespace ArchLord2Commons.Network.Cryptography
{
    public class Session
    {
        protected int Key;

        public Session(int key = 0x11cfd)
        {
            Key = key;
        }

        public byte[] Encrypt(ref byte[] buffer)
        {
            int param = Key;

            for (int i = 0; i < buffer.Length; i++)
            {
                buffer[i] ^= (byte)(param >> 8);
                param = ((buffer[i] & 0xff) + param) * 0x0CE6D + 0x58BF;
            }

            return buffer;
        }

        public byte[] Decrypt(ref byte[] buffer)
        {
            int param = Key;

            for (int i = 0; i < buffer.Length; i++)
            {
                byte stt = buffer[i];

                buffer[i] ^= ((byte)(param >> 8));
                param = (((stt & 0xff) + param) * 0x0CE6D + 0x58BF);
            }

            return buffer;
        }
    }
}
